//
//  WKPublishQuestionVC.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/26.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKPublishQuestionVC.h"
#import "WKSubmitQuestionCell.h"

@interface WKPublishQuestionVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray <UIImage *>*imgs;
@end

@implementation WKPublishQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
}

- (void)initViews{
    self.view.backgroundColor = [UIColor whiteColor];
    self.naviBar.title = @"提问题";
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight-NavBarHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:self.tableview];
     NSArray *identifierArr = @[@"QUESTIONTITLE",@"QUESTIONTAG",@"QUESTIONTEXT",@"QUESTIONSAN",@"QUESTIONPHOTO",@"QUESTIONYAO"];
    for (int i = 0; i<identifierArr.count; i++) {
        NSString *identifier = identifierArr[i];
        [_tableview registerClass:[WKSubmitQuestionCell class] forCellReuseIdentifier:identifier];
    }
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
  NSArray *identifierArr = @[@"QUESTIONTITLE",@"QUESTIONTAG",@"QUESTIONTEXT",@"QUESTIONSAN",@"QUESTIONPHOTO",@"QUESTIONYAO"];
    NSString *identifier = identifierArr[indexPath.row];
    WKSubmitQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row==4) {
        @weakify(self);
        cell.selectImgBlock = ^(NSArray * _Nonnull imgs) {
            @strongify(self);
            self.imgs = imgs;
            NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
            [tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationFade)];
        };
        [cell displayImgWithImgArr:self.imgs];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==2) {
        return 150;
    }
    if (indexPath.row==4) {
        
        CGFloat height = 0;
        CGFloat itemHeigth = (ScreenWidth - 24 -10)/3;
        if (self.imgs.count>=3 && self.imgs.count<6) {
            
            height = 46 + itemHeigth*2+10+5;
        }else if(self.imgs.count>=6){
            height = 46 + itemHeigth*3+10+5*2;
        }else{
            height = 46 + itemHeigth + 10;
        }
        return height+30;
        
    }
    
    if (indexPath.row == 3) {
        return 46+24;
    }
    
    return 46;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
